<?php
declare(strict_types=1);
/**
 * MineAdmin is committed to providing solutions for quickly building web applications
 * Please view the LICENSE file that was distributed with this source code,
 * For the full copyright and license information.
 * Thank you very much for using MineAdmin.
 *
 * @Author X.Mo<root@imoi.cn>
 * @Link   https://gitee.com/xmo/MineAdmin
 */

namespace Plugin\Attractions\Broadcast\Repository\Broadcast_devices;

use Plugin\Attractions\Broadcast\Model\Broadcast_devices\BroadcastDevices;
use Hyperf\Database\Model\Builder;
use App\Repository\IRepository;
use Hyperf\Collection\Arr;

/**
 * 广播设备管理 Repository类
 */
class BroadcastDevicesRepository extends IRepository
{
   public function __construct(
        protected readonly BroadcastDevices $model
    ) {}

    /**
     * 搜索处理器
     * @param Builder $query
     * @param array $params
     * @return Builder
     */
    public function handleSearch(Builder $query, array $params): Builder
    {
        
        // 主键ID
        if (isset($params['id']) && !empty($params['id'])) {
            $query->whereIn('id', Arr::wrap($params['id']));
        }

        // 设备名称
        if (isset($params['device_name']) && !empty($params['device_name'])) {
            $query->where('device_name', 'like', '%'.$params['device_name'].'%');
        }

        // 设备型号
        if (isset($params['device_model']) && !empty($params['device_model'])) {
            $query->where('device_model', 'like', '%'.$params['device_model'].'%');
        }

        // 设备品牌
        if (isset($params['device_brand']) && !empty($params['device_brand'])) {
            $query->where('device_brand', 'like', '%'.$params['device_brand'].'%');
        }

        // 设备序列号
        if (isset($params['device_sn']) && !empty($params['device_sn'])) {
            $query->where('device_sn', 'like', '%'.$params['device_sn'].'%');
        }

        // 经度
        if (isset($params['longitude']) && !empty($params['longitude'])) {
            $query->where('longitude', '=', $params['longitude']);
        }

        // 纬度
        if (isset($params['latitude']) && !empty($params['latitude'])) {
            $query->where('latitude', '=', $params['latitude']);
        }

        // 海拔高度
        if (isset($params['altitude']) && !empty($params['altitude'])) {
            $query->where('altitude', '=', $params['altitude']);
        }

        // IP地址
        if (isset($params['ip_address']) && !empty($params['ip_address'])) {
            $query->where('ip_address', 'like', '%'.$params['ip_address'].'%');
        }

        // 端口号
        if (isset($params['port']) && !empty($params['port'])) {
            $query->where('port', '=', $params['port']);
        }

        // 密钥
        if (isset($params['secret']) && !empty($params['secret'])) {
            $query->where('secret', 'like', '%'.$params['secret'].'%');
        }

        // MAC地址
        if (isset($params['mac_address']) && !empty($params['mac_address'])) {
            $query->where('mac_address', 'like', '%'.$params['mac_address'].'%');
        }

        // 在线状态：1-离线，2-在线
        if (isset($params['online_status']) && !empty($params['online_status'])) {
            $query->where('online_status', '=', $params['online_status']);
        }

        // 最后在线时间
        if (isset($params['last_online_time']) && !empty($params['last_online_time']) && is_array($params['last_online_time']) && count($params['last_online_time']) == 2) {
            $query->whereBetween(
                'last_online_time',
                [ $params['last_online_time'][0], $params['last_online_time'][1] ]
            );
        }

        // 设备分组
        if (isset($params['device_group']) && !empty($params['device_group'])) {
            $query->where('device_group', 'like', '%'.$params['device_group'].'%');
        }

        // 维护状态：1-正常，2-需要维护，3-维护中，4-报废
        if (isset($params['maintenance_status']) && !empty($params['maintenance_status'])) {
            $query->where('maintenance_status', '=', $params['maintenance_status']);
        }

        // 备注
        if (isset($params['remark']) && !empty($params['remark'])) {
            $query->where('remark', '=', $params['remark']);
        }

        return $query;
    }

    public function getOnlineByGroup($group = null)
    {
        return $this->model->query()->when($group, function ($query) use ($group) {
            $query->where('device_group', '=', $group);
        })
            ->where('online_status', '=', 2)
            ->get()
            ->groupBy('device_group')
            ->map(function ($group) {
                return $group->map(function ($item) {
                    return $item['id'];
                });
            })
            ->toArray();
    }
}